package com.jowell.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jowell.common.utils.CodeMsg;
import com.jowell.common.utils.PageUtils;
import com.jowell.common.utils.Query;
import com.jowell.common.utils.Result;
import com.jowell.mall.product.dao.CategoryDao;
import com.jowell.mall.product.entity.CategoryEntity;
import com.jowell.mall.product.service.CategoryService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * @param [params]
     * @Author: Mr.ZJW
     * @Description: 查询所有类别数据，然后将数据封装为树形结构
     * @Date: 2022/6/15 13:51
     */
    @Override
    public List<CategoryEntity> listTree(Map<String, Object> params) {
        //1.查询所有类别数据
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        //2.将查询的数据拆解为树形结构【父子关系】
        //2.1遍历大类
        return categoryEntities.stream()
                .filter(categoryEntity -> categoryEntity.getParentCid() == 0)
                .map(categoryEntity -> {
                    //根据大类找到中类以及小类 递归方式，便于后面增加类目等级
                    categoryEntity.setChildrens(getCategoryChildrens(categoryEntity, categoryEntities));
                    return categoryEntity;
                }).sorted((entity1, entity2) -> {
                    return (entity1.getSort() == null ? 0 : entity1.getSort()) - (entity2.getSort() == null ? 0 : entity2.getSort());
                }).collect(Collectors.toList());
    }

    /**
     * @param categoryEntity   某个大类
     * @param categoryEntities 所有类别数据
     * @Author: Mr.ZJW
     * @Description: 查找该大类下的小类
     * @Date: 2022/6/15 14:38
     **/
    private List<CategoryEntity> getCategoryChildrens(CategoryEntity categoryEntity, List<CategoryEntity> categoryEntities) {
        return categoryEntities.stream().filter(entity -> {
            //根据大类找到他直属小类
            return entity.getParentCid().equals(categoryEntity.getCatId());
        }).map(entity -> {
            //在根据这个小类找到对应的小类【递归方式】
            entity.setChildrens(getCategoryChildrens(entity, categoryEntities));
            return entity;
        }).sorted((entity1, entity2) -> {   //排序
            return (entity1.getSort() == null ? 0 : entity1.getSort()) - (entity2.getSort() == null ? 0 : entity2.getSort());
        }).collect(Collectors.toList());
    }

    /**
     * @Author: Mr.ZJW
     * @Description: 逻辑删除
     * @Date: 2022/6/24 14:31
     **/
    @Override
    public Result removeCategoryByIds(List<Long> asList) {
        int update = baseMapper.deleteBatchIds(asList);
        if (update <= 0) {
            return Result.error("删除失败");
        }
        return Result.ok("删除成功");
    }

    /**
     * @param category
     * @Author: Mr.ZJW
     * @Description: 批量修改
     * @Date: 2022/6/28 15:17
     **/
    @Override
    public Result BatchUpdate(List<CategoryEntity> category) {
        //更新
        boolean b = this.updateBatchById(category);
        if (!b) {
            return Result.error(CodeMsg.UPDATE_ERROR);
        }
        return Result.ok(CodeMsg.UPDATE_SUCCESS);
    }

}